package com.shuqi.model.manager;

import java.util.HashSet;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import android.text.TextUtils;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.database.dao.impl.BookCataLogDao;
import com.shuqi.database.model.BookCataLog;
import com.shuqi.model.bean.BookChapterOssInfo;

public class CacheChapters {

    private static CacheChapters instance;
    private ExecutorService mPool;
    private CacheChapterThread mDownLoadThread;

    private CacheChapters() {
        mPool = Executors.newSingleThreadExecutor();
    }

    public static synchronized CacheChapters getInstance() {
        if (instance == null) {
            instance = new CacheChapters();
        }
        return instance;
    }

    public synchronized void notifyCacheBook(String sourceId, String bookId, String uid,
            HashSet<String> cidSet) {
        if (mDownLoadThread != null) {
            mDownLoadThread.setStatus(false);
        } 
        mDownLoadThread = new CacheChapterThread(sourceId, bookId, uid, cidSet);
        mPool.execute(mDownLoadThread);
    }
    
    public void destory(){
        mPool.shutdown();
    }

    class CacheChapterThread implements Runnable {
        private AtomicBoolean status;
        private String mSourceId;
        private String mBookId;
        private String mUid;
        private Vector<String> mdownLoadIds = new Vector<String>();

        public CacheChapterThread(String sourceId, String bookId, String uid, HashSet<String> cidSet) {
            mdownLoadIds.clear();
            mdownLoadIds.addAll(cidSet);
            mSourceId = sourceId;
            mBookId = bookId;
            mUid = uid;
            status = new AtomicBoolean(true);
        }

        public void setStatus(boolean value) {
            status.set(value);
        }


        @Override
        public void run() {
            while (status.get() && mdownLoadIds.size()>0) {
                String cid = mdownLoadIds.get(0);
                mdownLoadIds.remove(0);
                if (cid != null) {
                    downLoadChapter(cid);
                } else {
                    break;
                }
            }
        }

        private void downLoadChapter(String cid) {
            BookCataLog catalogInfo =
                    BookCataLogDao.getInstance().getBookCatalogByCid(mUid, mBookId, mSourceId, cid);
            String chapterInfo = null;
            if (catalogInfo != null) {
                if (mSourceId == null) {
                    List<String> chapterContent =
                            DownLoadBookManager.loadPayChapterContent(
                                    ShuqiApplication.getContext(),
                                    catalogInfo.getChapterContentUrl());
                    if (chapterContent != null && chapterContent.size() > 0) {
                        chapterInfo = chapterContent.get(0);
                    }
                } else {
                    BookChapterOssInfo chapterContent =
                            DownLoadBookManager.loadYiSouChapterContent(
                                    ShuqiApplication.getContext(),
                                    catalogInfo.getChapterContentUrl());
                    if (chapterContent != null) {
                        chapterInfo = chapterContent.getText();
                    }
                }
            }


            if (!TextUtils.isEmpty(chapterInfo)) {
                // 保存文件
                DownLoadBookManager.writeChapterFile(mSourceId, mBookId, mUid, cid, chapterInfo);
                // 更新为已下载
                BookCataLogDao.getInstance().updateCatalogToDown(mBookId, mSourceId, mUid, cid);
                // 更新为已付费
//                BookCataLogDao.getInstance().updateCatalogToPaid(mBookId, mSourceId, mUid, cid);
            }
        }
    }
}
